package com.codeoss.framework.impexp;

import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.converter.IConverterStore;
import com.codeoss.framework.data.metadata.TableData;
import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.IScriptServiceStore;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.IDataBinding;
import com.codeoss.framework.view.component.ILabel;
import com.codeoss.framework.view.component.Table;
import com.codeoss.framework.view.component.TableColumn;
import com.codeoss.framework.view.model.SelectItem;

@Service
public class ExcelTableComponentExport implements ITableComponentExport {
    @Autowired
    private IConverterStore converterStore;
    @Autowired
    private IScriptServiceStore scriptServiceStore;
	@Override
	public void export(YunContext yunContext,Table table,TableData tableData,Map<String, List<SelectItem>> selectItemMap,OutputStream os) throws IOException {
		// TODO Auto-generated method stub
		SXSSFWorkbook workbook = new SXSSFWorkbook();//
		workbook.setCompressTempFiles(true);

		CellStyle titleStyle = workbook.createCellStyle();
		titleStyle.setAlignment(HorizontalAlignment.CENTER);
		Font titleFont = workbook.createFont();
		titleFont.setBold(true);
		titleStyle.setFont(titleFont);

		SXSSFSheet sheet = workbook.createSheet();
		SXSSFRow titleRow = sheet.createRow(0);
		List<Component> columnList = table.getChildren();
		for (int i = 0; i < columnList.size(); i++) {
			SXSSFCell cell = titleRow.createCell(i);
			cell.setCellStyle(titleStyle);
			TableColumn column =(TableColumn)columnList.get(i);
			Component component=column.getChildren().get(0);
			if(component instanceof ILabel) {
				Expression expression=((ILabel)component).getLabelExpression();
				String label=scriptServiceStore.getScriptService(expression).eval(expression.getScript(), null);
				cell.setCellValue(label);
			}
		}
	    List<Map<String,Object>> dataList=tableData.getData();
		for(int i=0;i<dataList.size();i++) {
			Map<String,Object> dataMap=dataList.get(i);
			SXSSFRow row = sheet.createRow(i+1);
			for (int j = 0; j< columnList.size(); j++) {
				TableColumn column =(TableColumn)columnList.get(j);
				Component component=column.getChildren().get(0);
				if(component instanceof IDataBinding) {
					Object value=dataMap.get(((IDataBinding)component).getDataField());
					if(value!=null) {
						SXSSFCell cell=row.createCell(j);
						if(value instanceof Timestamp) {
							value=converterStore.getConverter("datetime").convertObjectToString(value);
							cell.setCellValue((String)value);
						}else if(value instanceof Date) {
							value=converterStore.getConverter("date").convertObjectToString(value);
							cell.setCellValue((String)value);
						}else if(value instanceof Time) {
							value=converterStore.getConverter("time").convertObjectToString(value);
							cell.setCellValue((String)value);
						}else if(value instanceof BigDecimal) {
							value=((BigDecimal)value).doubleValue();
							cell.setCellValue((double)value);
						}else if(value instanceof Long) {
							List<SelectItem> selectItem=selectItemMap.get(component.getId());
							if(selectItem!=null) {
								long longValue=((Long)value).longValue();
								for(int k=0;k<selectItem.size();k++) {
									if(((Long)selectItem.get(k).getValue()).longValue()==longValue) {
										cell.setCellValue(selectItem.get(k).getName());
										break;
									}
								}
							}else {
								BigDecimal bd=new BigDecimal((Long)value);
								value=bd.doubleValue();
								cell.setCellValue((double)value);
							}
						}else if(value instanceof Integer) {
							BigDecimal bd=new BigDecimal((Integer)value);
							value=bd.doubleValue();
							cell.setCellValue((double)value);
						}else if(value instanceof Boolean) {
							if((boolean)value==true) {
								cell.setCellValue("是");
							}else {
								cell.setCellValue("否");
							}
						}else if(value instanceof String) {
							List<SelectItem> selectItem=selectItemMap.get(component.getId());
							if(selectItem!=null) {
								for(int k=0;k<selectItem.size();k++) {
									if(((String)selectItem.get(k).getValue()).equals((String)value)){
										cell.setCellValue(selectItem.get(k).getName());
										break;
									}
								}
							}else {
								cell.setCellValue((String)value);
							}
						}
					}
				}
			}
		}
		workbook.write(os);
		workbook.close();
		workbook.dispose();
	}

}
